package com.xzw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xzw.mapper.StudentMapper;
import com.xzw.pojo.PageResult;
import com.xzw.pojo.StuQueryParam;
import com.xzw.pojo.Student;
import com.xzw.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 学员列表 条件分页查询
     */
    @Override
    public PageResult<Student> page(StuQueryParam stuQueryParam) {
        // 设置分页参数
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());
        // 执行查询
        List<Student> studentList = studentMapper.list(stuQueryParam);
        // 解析查询结果，并封装
        Page<Student> p = (Page<Student>) studentList;
        return new PageResult<Student>(p.getTotal(), p.getResult());
    }
    /**
     * 根据ID批量 删除学员
     */
    @Override
    public void delete(List<Integer> idList) {
        studentMapper.delete(idList);
    }
    /**
     * 添加学员
     */
    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }
    /**
     * 根据ID查询学员 查询回显功能
     */
    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getById(id);
    }
    /**
     * 修改学员信息
     */
    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }
    /**
     * 违纪处理
     */
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id, score);
    }
}
